package com.beiding.workordersystem.main.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.beiding.workordersystem.common.standard.E;
import com.beiding.workordersystem.main.option.OptionAble;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

//选项分组
@Data
@TableName("bus_option")
public class Option {


    //id
    @TableId
    private String id;

    //映射的名称
    private String typeName;

    //父级节点
    private String pid;

    //层级
    private Integer hierarchy;


    @TableField(exist = false)
    private Class<? extends OptionAble> type;

    //字段
    @TableField(exist = false)
    private List<OptionItem> items;

    public Object set(String key, Object value) {
        E.notNull(key,"键不能为空");
        if (items == null) {
            items = new ArrayList<>();
        }
        OptionItem item = null;
        for (OptionItem optionItem : items) {
            if (optionItem.getTheKey().equals(key)) {
                item = optionItem;
                break;
            }
        }
        if (item == null) {
            item = new OptionItem();
            item.setTheKey(key);
            items.add(item);
        }
        Object r = item.getValue();
        item.setValue(value);
        return r;
    }

    public Object get(String key) {
        E.notNull(key,"键不能为空");
        if (items == null) {
            return null;
        }

        for (OptionItem item : items) {
            if (item.getTheKey().equals(key)) {
                return item.getValue();
            }
        }
        return null;
    }

}
